<%@ page contentType="text/html;charset=UTF-8" %>
<%@include file="/mb/jsp/common.jsp" %>

<%@ page language="java" import="java.util.*" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page language="java" import="java.io.*" %>
<%@ page language="java" import="com.wsu.search.tools.Tools" %>
<%@ page language="java" import="com.limao.Tool.CacheClass" %>
<%@ page language="java" import="com.limao.CacheManager.sys_AdvCacheManager" %>
<%@ page language="java" import="com.limao.DBDataClass.AdvDataClass" %>
<%@ page language="java" import="com.limao.CacheManager.CrowdSysCateCache" %>
<%@ page language="java" import="com.limao.DBDataClass.SysProjectCateDataClass" %>
<%@ page language="java" import="com.limao.CacheManager.CrowdHomeCommendCache" %>
<%@ page language="java" import="com.limao.DBDataClass.CrowdHomeCommendClass" %>
<%@ page language="java" import="com.limao.DBDataClass.CrowdSysProjectClass" %>

<%
    /*
     网站首页生成程序
     */

    //取得url带过来的参数
    // String v = Tools.RmNull(request.getParameter("v"));
    String v = "show";

    /*************读写模板文件操作*****************/

    //读模板,先判断此信息最终极分类有没有,没有读t_type c_belong根栏目分类模板,还没有读news_info.html资讯类默认模板
//    String filename = request.getRealPath("model/mod_index_mb.html");
//    System.out.println(filename);
//    File fileobj = new File(filename);
//    if (!fileobj.exists()) {
//        //如果模板文件找不到
//        out.println("<font color=red>出错了,模板mod_index_mb.html文件找不到或丢失!</font>");
//        return;
//    }
//    StringBuffer strb = Tools.readFile(filename);

    /************* 广告图 *****************/

    //先调 用接口取得广告图数据
    List<AdvDataClass> advlist = sys_AdvCacheManager.getInstance().Load_SysHomeAdv();
    //数据与样式进行结合，组装
    String tmpStrAdvround = "", tmpStrAdvImage = "";
    int index = 1;
    if (advlist != null && advlist.size() > 0) {
        for (AdvDataClass obj : advlist) {
            tmpStrAdvround = tmpStrAdvround + "<a href=\"" + "\">" + index + "</a>";  //广告圆点
            if (CacheClass.getIntValue(obj.type) == 0) //跳转网页
                tmpStrAdvImage = tmpStrAdvImage + "<li><a href=\"" + obj.weburl + "\" ><span style=\"background:url(" + getImageBasePath(obj.imgurl) + ") center top no-repeat;background-size: cover;\"></span></li>";
            else  //跳转众筹项目
                tmpStrAdvImage = tmpStrAdvImage + "<li><a href=\"" + obj.weburl + "\" ><span style=\"background:url(" + getImageBasePath(obj.imgurl) + ") center top no-repeat;background-size: cover;\"></span></a></li>";
            index++;
        }
    }
    //数据填充
//    strb = Tools.replaceAll(strb, "[广告图圆点]", tmpStr1);
//    strb = Tools.replaceAll(strb, "[广告图图片]", tmpStr2);


    /***************** 菜单项  *****************/

    //先调 用接口取得众筹分类列表数据 --《接口文档》
    List<SysProjectCateDataClass> catlist = CrowdSysCateCache.getInstance().loadProjectCateJson();
    //数据与样式进行结合，组装
    String tmpStrFastMenu = "";
    index = 0;
    if (catlist != null && catlist.size() > 0) {
        for (SysProjectCateDataClass obj : catlist) {
            if (index == 7) { //当有8个，且为第8个时,在最后加 其它并中断for
                tmpStrFastMenu = tmpStrFastMenu + "<a href=\"mb/jsp/classification.jsp?cateid=" + obj.cateid + "\"><img src=\"mb/images/qita.png\" class=\"navImg\"><i>其他</i></a></div>";
                break;
            }
            int mod = index % 4; // 一行4个  开头
            if (mod == 0) {
                tmpStrFastMenu = tmpStrFastMenu + "<div class=\"w100 clearfix of\" style=\"margin-top:2px;\">";
            }
            tmpStrFastMenu = tmpStrFastMenu + "<a href=\"mb/jsp/classification.jsp?cateid=" + obj.cateid + "\"><img src=\"" + getImageBasePath(obj.cateimage) + "\" class=\"navImg\"><i>" + obj.catename + "</i></a>";

            index++;
            if (index % 4 == 0)  // 一行4个  结尾
            {
                tmpStrFastMenu = tmpStrFastMenu + "</div>";
            }
        }
        //当不足8个时,在最后加 其它
        if (index < 7) {
            tmpStrFastMenu = tmpStrFastMenu + "<a href=\"mb/jsp/classification.jsp?cateid=" + "8" + "\"><img src=\"mb/images/qita.png\" class=\"navImg\"><i>其他</i></a></div>";
        }
    }
    // System.out.println(tmpStr);
    //数据填充
    // strb = Tools.replaceAll(strb, "[首页快捷菜单]", tmpStr);


    /***************** 众筹首页推荐列表     *****************/

    //先调 用接口取得众筹主页推荐列表数据 --《接口文档》
    List<CrowdHomeCommendClass> columList = CrowdHomeCommendCache.getInstance().Load_CrowdHomeCommend_All();
    //数据与样式进行结合，组装
    String tmpStrTuijianList = "";
    if (columList != null && columList.size() > 0) {
        for (CrowdHomeCommendClass obj : columList) {
            String cateid = obj.cateid;
            String image = obj.cateimage;
            String catename = obj.catename;
            tmpStrTuijianList = tmpStrTuijianList + "<section class=\" w100 of\" style=\"margin: 6px auto 2px;\"><a class=\"fl indexKindLable f16\"><img src=\"" + getImageBasePath(image) + "\" class=\"dib pr\">" + catename + "</a><a href=\"mb/jsp/classification.jsp?cateid=" + cateid + "\" class=\"indexKindMore fr f14 tc\">更多...</a></section>";

            List<CrowdSysProjectClass> childlist = obj.childlist;
            if (columList != null && columList.size() > 0) {
                for (CrowdSysProjectClass tmp : childlist) {
                    int state = CacheClass.getIntValue(tmp.state);
                    String stateStr = "";
                    //状态(0下架，1预热，2众筹中，3众筹成功) -> (0下架，1预热，2推荐，3热销，4新品)
                    switch (state) {
                        case 0:
                            stateStr = "下架";
                            break;
                        case 1:
                            stateStr = "预热";
                            break;
                        case 2:
                            stateStr = "推荐";
                            break;
                        case 3:
                            stateStr = "热销";
                        case 4:
                            stateStr = "新品";
                            break;
                    }
                    String projectidStr = tmp.projectid;
                    String supportnumStr = tmp.supportnum;
                    String projectimageStr = tmp.projectimage;
                    if (!CacheClass.isEmpty(projectimageStr))
                        projectimageStr = projectimageStr.split(";")[0];
                    String projectnameStr = tmp.projectname;
                    String companynameStr = tmp.companyname;
                    String packagepriceStr = "￥" + tmp.packageprice;
                    
                    
                    int stocknum=0; 
				    if((CacheClass.getIntValue(tmp.limitenum)-CacheClass.getIntValue(tmp.supportnum))>0)
				          stocknum=CacheClass.getIntValue(tmp.limitenum)-CacheClass.getIntValue(tmp.supportnum);
				    String nowrmbStr = stocknum+"";
                    //String nowrmbStr = "￥" + tmp.nowrmb;
                    
                    
                    String dayStr = tmp.days + "&nbsp;天";


                    tmpStrTuijianList = tmpStrTuijianList + "<section class=\"proSection pr w100 bcf\" onclick=\"window.location.href='mb/jsp/productDetail.jsp?projectid=" + projectidStr + "'\">" +
                            "<div class=\"proTittle pa w100\"><div class=\"of clearfix w100\"><span class=\"fl\">" + stateStr + "</span><a class=\"fr f12 aRight\"><img src=\"mb/images/zan.png\" class=\"zan dib hauto pr\">" + supportnumStr + "</a></div></div>" +
                            "<a class=\"proImg db w100 of\"><img src=\"" + getImageBasePath(projectimageStr) + "\" class=\"db w100 h100\"></a>" +
                            "<a class=\"proName db of clearfix\"><span class=\"fl\">" + projectnameStr + "</span><i class=\"fr\">" + companynameStr + "</i></a>" +
    
                            "<a class=\"progress db\"><div class=\"bar_span\" width=\"100%\"></div></a>" +
                            "<a class=\"proDetail db of clearfix\"><div class=\"fl tl\" style=\"width: 45%\"><span class=\"db f14\">" + packagepriceStr + "</span><i class=\"db f12\">价格</i></div>" +
                            "<div class=\"fl tl\" style=\"width: 35%\"><span class=\"db f14\">" + nowrmbStr + "</span><i class=\"db f12\">库存</i></div>" +
                            "<div class=\"fl tc\" style=\"width: 20%\"><span class=\"db f14\">" + dayStr + "</span><i class=\"db f12\">送vip</i></div></a></section>";

                }
            }


        }

    }
    //System.out.println(tmpStrTuijianList);
    //数据填充
    //strb = Tools.replaceAll(strb, "[首页推荐列表]", tmpStr);

    /***************** 输出网页到客户端或写入到某html网页作为静态化     *****************/


//    if (v.equals("show")) {    //预览首页,即直接输出网页到客户端
//        out.println(strb);
//    } else {  //生成首页  静态化
//        String modstr = strb.toString();
//        String strFileName = request.getRealPath("/mb/index.html"); //准备保存的静态网页路径及文件名
//        //System.out.println(strFileName);
//        try {
//            FileOutputStream afile = new FileOutputStream(strFileName);
//            afile.write(modstr.getBytes("UTF-8"));
//            afile.close();
//
//            modstr = null;
//        } catch (IOException e) {
//            out.println(e.getMessage());
//        }
//    }

   // strb = null;

%>

<html lang="en">
<head>
    <meta charset="UTF-8">

    <title></title>
    <script>
		$(window).on("load", function() {
				console.log("868");
				headertabcurrent=1;
				$("#footer").load("mb/footer.jsp"); 
				//headerTab(1);
	    })
	</script>
    <script type="text/javascript">
        var timer = null;

        $(document).ready(function () {
//            图片滑动
            $dragBln = false;
            $(".main_image").touchSlider({
                flexible: true,
                speed: 200,
                btn_prev: $("#btn_prev"),
                btn_next: $("#btn_next"),
                paging: $(".flicking_con a"),
                counter: function (e) {
                    $(".flicking_con a").removeClass("on").eq(e.current - 1).addClass("on");
                }
            });

            $(".main_image").bind("mousedown", function () {
                $dragBln = false;
            });

            $(".main_image").bind("dragstart", function () {
                $dragBln = true;
            });

            $(".main_image a").click(function () {
                if ($dragBln) {
                    return false;
                }
            });
            $(".main_visual").hover(function () {
//                clearInterval(timer);
            }, function () {
                timer = setInterval(function () {
                    $("#btn_next").click();
                }, 5000);
            });

            $(".main_image").bind("touchstart", function () {
                clearInterval(timer);
            }).bind("touchend", function () {
                timer = setInterval(function () {
                    $("#btn_next").click();
                }, 5000);
            });

            //            进度条
            $(".bar_span").each(function () {
                var ress = $(this).attr('width');
                var len = ress + "%";
                console.log(len);
                $(this).animate({
                    width: len
                });

            })


        });
    </script>

</head>
<body ontouchstart="">
<div class="container jz">
    <div class="main_visual">
        <div class="flicking_con w100">
            <div class="dib jz">
                <%
                    out.print(tmpStrAdvround);
                %>
            </div>
        </div>
        <div class="main_image">
            <ul>
                <%
                    out.print(tmpStrAdvImage);
                %>
            </ul>
        </div>
    </div>
    <!--main_visual-->
    <nav class="w100" id="main_visual" style="padding-top:5px;padding-bottom:5px;">
        <%
           // out.print(tmpStrFastMenu); 
            System.out.println(tmpStrFastMenu);
        %>
    </nav>
    <p class="w100" style="background: #E7E7E7;height: 1px;"></p>
    <p class="w100" style="background: #FFFFFF";height: 1px;></p>
    <%
        out.print(tmpStrTuijianList);
    %>
</div>

  	<!--footer-->
<footer id="footer" class="footerTab w100 of pf">
 
</footer>

<script>

   
    window.addEventListener('pageshow', function (event) {
        // 获取localStore中的变量
        var needrefresh = localStorage.getItem("needrefreshindex"); //存储变量名为key，值为value的变量
        if (needrefresh != '' && needrefresh != null && needrefresh == 'true') {
            localStorage.removeItem("needrefreshindex");
            window.location.reload();
        }
    });

$(document).ready(function(){
	$("#main_visual").html('');
	$("#main_visual").html('<%=tmpStrFastMenu%>');

});


</script>

</body>
</html>